package com.gxnu.edu.cn.ttxxt.controller;

import com.gxnu.edu.cn.ttxxt.entity.Favorites;
import com.gxnu.edu.cn.ttxxt.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/favorites")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    @PostMapping("/")
    public ResponseEntity<?> addFavorite(@RequestBody Map<String, Object> request) {
        Long userId = Long.parseLong(request.get("userId").toString());
        Long contactId = Long.parseLong(request.get("contactId").toString());
        Integer contactType = Integer.parseInt(request.get("contactType").toString());

        favoriteService.addFavorite(userId, contactId, contactType);
        return ResponseEntity.ok("已添加到常用联系人");
    }

    @DeleteMapping("/")
    public ResponseEntity<?> removeFavorite(@RequestBody Map<String, Object> request) {
        Long userId = Long.parseLong(request.get("userId").toString());
        Long contactId = Long.parseLong(request.get("contactId").toString());

        favoriteService.removeFavorite(userId, contactId);
        return ResponseEntity.ok("已从常用联系人移除");
    }

    @PutMapping("/order")
    public ResponseEntity<?> updateFavoriteOrder(@RequestBody Map<String, Object> request) {
        Long userId = Long.parseLong(request.get("userId").toString());
        Long contactId = Long.parseLong(request.get("contactId").toString());
        Integer sortOrder = Integer.parseInt(request.get("sortOrder").toString());

        favoriteService.updateFavoriteOrder(userId, contactId, sortOrder);
        return ResponseEntity.ok("常用联系人排序已更新");
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<?> getUserFavorites(@PathVariable Long userId) {
        List<Favorites> favorites = favoriteService.getUserFavorites(userId);
        return ResponseEntity.ok(favorites);
    }
}
    